package org.intellij.doctree;

import org.intellij.doctree.node.Folder;
import org.intellij.doctree.node.FolderNode;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class FolderDialog extends NodeDialog {

    protected JTextField folderName;
    protected JTextField folderDesc;

    public FolderDialog(Component parent) throws HeadlessException {
        super(parent);
        newNode = true;
        init();
    }

    public FolderDialog(Component parent, FolderNode folderNode) throws HeadlessException {
        super(parent);
        init();
        setFolder(folderNode.getFolder());
    }

    protected void init() {
        super.init();
        setTitle(newNode ? "New Folder" : "Edit Folder");
        pack();
    }

    @Nullable
    protected JComponent createCenterPanel() {

        JLabel folderNameLbl = new JLabel("Folder Name:");
        JLabel folderDescLbl = new JLabel("Description:");
        folderName = new JTextField("");
        folderName.setColumns(32);
        folderDesc = new JTextField("");
        folderDesc.setColumns(32);

        JPanel fieldPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c;

        c = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0);
        fieldPanel.add(folderNameLbl, c);

        c = new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0);
        fieldPanel.add(folderName, c);

        c = new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0);
        fieldPanel.add(folderDescLbl, c);

        c = new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0);
        fieldPanel.add(folderDesc, c);

        c = new GridBagConstraints(0, 2, 2, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0);
        fieldPanel.add(new JPanel(), c);

        return fieldPanel;
    }

    public String getFolderName() {
        return folderName.getText();
    }

    public String getFolderDesc() {
        return folderDesc.getText();
    }

    public Folder getValue() {
        return getFolder();
    }

    protected boolean isValidated() {
        String name = folderName.getText().trim();
        if (name.length() == 0) {
            JOptionPane.showMessageDialog(this.getWindow(), "Folder name cannot be blank",
                    "Folder Details", JOptionPane.ERROR_MESSAGE);
            folderName.requestFocus();
            return false;
        }
        return true;
    }

    public Folder getFolder() {
        return new Folder(folderName.getText().trim(), folderDesc.getText().trim());
    }

    public void setFolder(Folder folder) {
        String name = folder == null ? "" : folder.getName();
        String desc = folder == null ? "" : folder.getDescription();
        folderName.setText(name);
        folderDesc.setText(desc);
    }

    public JComponent getPreferredFocusedComponent() {
        return folderName;
    }

}
